package zero.tech.games.dn.util;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import zero.tech.games.dn.data.DnConfig;
import zero.tech.games.dn.data.DnGameBase;
import zero.tech.games.dn.data.DnResGameBase;
import zero.tech.games.dn.data.DnResReconnect;
import zero.tech.games.dn.data.DnRoomStatus;
import zero.tech.games.dn.data.DnTimer;
import zero.tech.games.dn.data.entity.DnOut;
import zero.tech.games.dn.data.entity.LiangpaiData;
import zero.tech.games.dn.data.entity.PlayerData;
import zero.tech.games.dn.data.entity.QiangzhuangData;
import zero.tech.games.dn.data.entity.YazhuData;
import zero.tech.games.dn.data.notify.NotifyBeginLiangpai;
import zero.tech.games.dn.data.notify.NotifyBeginQiangzhuang;
import zero.tech.games.dn.data.notify.NotifyBeginYazhu;
import zero.tech.games.dn.data.notify.NotifyGameStart;
import zero.tech.games.dn.data.notify.NotifySomeoneLiangpai;
import zero.tech.games.dn.data.notify.NotifySomeoneYazhu;
import zero.tech.games.dn.data.notify.NotifySomoneQiangzhuang;
import zero.tech.games.dn.data.res.ResLiangpai;
import zero.tech.games.dn.data.res.ResQiangzhuang;
import zero.tech.games.dn.data.res.ResSetcards;
import zero.tech.games.dn.data.res.ResSetcardsLeft;
import zero.tech.games.dn.data.res.ResYazhu;
import zero.tech.games.logic.common.Room;
import zero.tech.games.logic.data.res.ResGameBase;
import zero.tech.games.logic.data.res.ResReconnet;
import zero.tech.games.logic.util.PckUtil;
import zero.tech.wsnet.controller.LogicController;
@Component
public class PackageUtil {

	public static NotifyGameStart packageNotifyGameStart(Room room) {
		NotifyGameStart notifyGameStart = new NotifyGameStart();
		DnGameBase dnGameBase = (DnGameBase) room.getGameBase();
		Map<Integer,PlayerData> playerDatas = dnGameBase.getPlayers();
		for(PlayerData pData:playerDatas.values()) {
			int key=pData.getUid();
			notifyGameStart.getHandsmap().put(key, pData.getShows());
		}
		return notifyGameStart;
	}

	public static ResReconnet packageResDnReconnect(Room room, LogicController online) {
		ResReconnet resReconnet=new DnResReconnect();
		PckUtil.packageResReconnect(room,online,resReconnet);
		int room_status=room.getRoom_status();
		if(room_status!=DnRoomStatus.DEFAULT&&room_status!=DnRoomStatus.CALCULATING) {
			resReconnet.setRes_gamebase(packageResPdkGameBase(room,online));
		}
		return resReconnet;
	}

	private static ResGameBase packageResPdkGameBase(Room room, LogicController online) {
		int uid=online.getId();
		int room_status=room.getRoom_status();
		long now=System.currentTimeMillis();
		DnResGameBase dnResGameBase = new DnResGameBase();
		DnGameBase dnGameBase = (DnGameBase) room.getGameBase();
		DnTimer dnTimer=(DnTimer) room.getTimer();
		DnConfig dnConfig=(DnConfig) room.getGameConfig();
		Map<Integer, PlayerData> playerDatas=dnGameBase.getPlayers();
		for(PlayerData pData:playerDatas.values()) {
			dnResGameBase.getShows().put(pData.getUid(),pData.getShows());
		}
		PlayerData playerData = playerDatas.get(uid);
		if(room_status==DnRoomStatus.QIANGZHUANG) {
			//抢庄阶段
			QiangzhuangData qiangzhuangData = new QiangzhuangData();
			int left_timer=(int) (dnConfig.getQiangzhuang_timer()-(now-dnTimer.lastBeginQiangzhuangTimer)/1000);
			left_timer=left_timer<0?0:left_timer;
			qiangzhuangData.setLeft_timer(left_timer);
			if(playerData!=null) {
				qiangzhuangData.setQiangzhuang_max_multiple(playerData.getQiangzhuang_max_multiple());
			}
			for(PlayerData pData:playerDatas.values()) {
				int key=pData.getUid();
				Integer val=pData.getQiangzhuang_multiple();
				if(val!=null) {
					qiangzhuangData.getQiangzhuangmap().put(key, val);
				}
			}
			dnResGameBase.setQiangzhuangData(qiangzhuangData);
		}
		if(room_status==DnRoomStatus.YAZHU) {
			//压注阶段
			YazhuData yazhuData = new YazhuData();
			int left_timer=(int) (dnConfig.getYazhu_timer()-(now-dnTimer.lastBeginYazhuTimer)/1000);
			left_timer=left_timer<0?0:left_timer;
			yazhuData.setLeft_timer(left_timer);
			if(playerData!=null) {
				yazhuData.setYazhu_max_multiple(playerData.getYazhu_max_multiple());
			}
			yazhuData.setBanker_uid(dnGameBase.getBanker_uid());
			for(PlayerData pData:playerDatas.values()) {
				int key=pData.getUid();
				Integer val=pData.getYazhu_multiple();
				if(val!=null) {
					yazhuData.getYazhumap().put(key, val);
				}
				Integer qiangzhuang_val=pData.getQiangzhuang_multiple();
				if(qiangzhuang_val!=null) {
					yazhuData.getQiangzhuangmap().put(key, qiangzhuang_val);
				}
				if(pData.getKanpai()!=null&&pData.getKanpai()) {
					yazhuData.getKanpailist().add(key);
				}
			}
			dnResGameBase.setYazhuData(yazhuData);
		}
		if(room_status==DnRoomStatus.LIANGPAI) {
			//亮牌阶段
			LiangpaiData liangpaiData = new LiangpaiData();
			int left_timer=(int) (dnConfig.getLiangpai_timer()-(now-dnTimer.lastBeginLiangpaiTimer)/1000);
			left_timer=left_timer<0?0:left_timer;
			liangpaiData.setLeft_timer(left_timer);
			liangpaiData.setBanker_uid(dnGameBase.getBanker_uid());
			for(PlayerData pData:playerDatas.values()) {
				int key=pData.getUid();
				Boolean val=pData.getLiangpai();
				if(val!=null) {
					DnOut dnOut = pData.getDnOut();
					liangpaiData.getLiangpaimap().put(key, dnOut);
				}
				Integer yazhu_val=pData.getYazhu_multiple();
				if(yazhu_val!=null) {
					liangpaiData.getYazhumap().put(key, yazhu_val);
				}
				if(pData.getKanpai()!=null&&pData.getKanpai()) {
					liangpaiData.getKanpailist().add(key);
				}
			}
			dnResGameBase.setLiangpaiData(liangpaiData);
		}
		return dnResGameBase;
	}

	public static NotifySomoneQiangzhuang packageNotifyQiangzhuang(int uid, int multiple) {
		NotifySomoneQiangzhuang notifyQiangzhuang = new NotifySomoneQiangzhuang();
		notifyQiangzhuang.setMultiple(multiple);
		notifyQiangzhuang.setUid(uid);
		return notifyQiangzhuang;
	}

	public static ResQiangzhuang packageResQiangzhuang(int uid, int multiple) {
		ResQiangzhuang resQiangzhuang = new ResQiangzhuang();
		resQiangzhuang.setMultiple(multiple);
		resQiangzhuang.setUid(uid);
		return resQiangzhuang;
	}

	public static NotifyBeginYazhu packageNotifyBeginYazhu(int banker_uid, int yazhu_timer,Integer yazhu_max_multiple) {
		NotifyBeginYazhu notifyBeginYazhu = new NotifyBeginYazhu();
		notifyBeginYazhu.setYazhu_max_multiple(yazhu_max_multiple);
		notifyBeginYazhu.setLeft_timer(yazhu_timer);
		notifyBeginYazhu.setBanker_uid(banker_uid);
		return notifyBeginYazhu;
	}

	public static ResYazhu packageResYazhu(int uid, int multiple) {
		ResYazhu resYazhu =new ResYazhu();
		resYazhu.setMultiple(multiple);
		resYazhu.setUid(uid);
		return resYazhu;
	}

	public static NotifySomeoneYazhu packageNotifyYazhu(int uid, int multiple) {
		NotifySomeoneYazhu notifySomeoneYazhu = new NotifySomeoneYazhu();
		notifySomeoneYazhu.setMultiple(multiple);
		notifySomeoneYazhu.setUid(uid);
		return notifySomeoneYazhu;
	}

	public static NotifyBeginLiangpai packageNotifyBeginLiangpai(int liangpai_timer) {
		NotifyBeginLiangpai notifyBeginLiangpai = new NotifyBeginLiangpai();
		notifyBeginLiangpai.setLeft_timer(liangpai_timer);
		return notifyBeginLiangpai;
	}

	public static ResLiangpai packageResLiangpai(int uid,DnOut dnOut) {
		ResLiangpai resLiangpai = new ResLiangpai();
		resLiangpai.setUid(uid);
		resLiangpai.setDnOut(dnOut);
		return resLiangpai;
	}

	public static NotifySomeoneLiangpai packageNotifyLiangpai(int uid, DnOut dnOut) {
		NotifySomeoneLiangpai notifySomeoneLiangpai = new NotifySomeoneLiangpai();
		notifySomeoneLiangpai.setUid(uid);
		notifySomeoneLiangpai.setDnOut(dnOut);
		return notifySomeoneLiangpai;
	}

	public static NotifyBeginQiangzhuang packageNotifyBeginQiangzhuang(Room room, PlayerData playerData) {
		DnConfig dnConfig = (DnConfig) room.getGameConfig();
		NotifyBeginQiangzhuang notifyBeginQiangzhuang = new NotifyBeginQiangzhuang();
		notifyBeginQiangzhuang.setLeft_timer(dnConfig.getQiangzhuang_timer());
		if(playerData!=null) {
			notifyBeginQiangzhuang.setQiangzhuang_max_multiple(playerData.getQiangzhuang_max_multiple());
		}
		return notifyBeginQiangzhuang;
	}

	public static ResSetcardsLeft packageResSetcardsLeft(List<Integer> cardIds) {
		ResSetcardsLeft resSetcardsLeft = new ResSetcardsLeft();
		resSetcardsLeft.getCardIds().addAll(cardIds);
		return resSetcardsLeft;
	}

	public static ResSetcards packageResSetcards(List<Integer> cardIds) {
		ResSetcards resSetcards = new ResSetcards();
		resSetcards.getCardIds().addAll(cardIds);
		return resSetcards;
	}

}
